// pages/userlogin/userlogin.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    USER_ASSETS_IDCARD: '', //手机号
    USER_ASSETS_NAME: '',
    ressj: '',
    timer: "", // 定时器名字
    countDownNum: '60',
    verify:"",
    flag: true, //设置节流
  },

  back(){
    // wx.switchTab({
    //   url: '../../pages/newshouye/newshouye',
    // })
    wx.navigateBack({
      delta: 1
    })
  },

  //登录接口
  login_a: function () {
    wx.request({
      url: app.globalData.interface+'/drainage/API_3007_DragonYearTwoRisingGoldLogin',
      method: 'POST',
      data: {
        USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
        code: this.data.verify,
        OPEN_ID: app.globalData.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data);
        this.setData({
          ressj: res.data
        })

        if (res.data.result == 'error') {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
              }
            }
          })
        } else { //往领券跳转时跳转带数据
          wx.navigateTo({
            // url: '../../pages/weihuyancaolingqu/weihuyancaolingqu?data=' + JSON.stringify(this.data.ressj)
            url: '../../pages/gedishishengjingzhuan/gedishishengjingzhuan?data=' + JSON.stringify(this.data.ressj)

          })
        }
      }
    })
    // }
  },

  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },


  fetch: function () {
    if (this.data.flag) {
      this.data.flag = false
    if (this.data.USER_ASSETS_IDCARD == '') {
      wx.showToast({
        title: '请填写手机号！',
        icon: 'none',
        duration: 3000
      })
    } else  if (this.data.session_key == "" || this.data.jiami == "" || this.data.session_key == null || this.data.jiami == null || this.data.session_key == undefined || this.data.jiami == undefined) {
      wx.showModal({
        title: '温馨提示',
        content: '数据错误请返回首页',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后   
            console.log('用户点击确定')

            wx.switchTab({
              url: '../../pages/newshouye/newshouye',
            })

          } else { //这里是点击了取消以后
            console.log('用户点击取消')
            wx.switchTab({
              url: '../../pages/newshouye/newshouye',
            })

          }
        }
      })
    } else{
      wx.request({
        url: app.globalData.interface+'/app/API_1052_sendMassage',
        method: 'GET',
        data: {
          phone: this.data.USER_ASSETS_IDCARD,
          KEY: 'xianjgjKey9999',
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key:this.data.session_key,
          OPEN_UUID:this.data.jiami,
          phone_type:'30'
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
   
          if (res.data.result == 'error') {
            wx.showToast({
              title: res.data.code,
              icon: 'none',
              duration: 3000
            })
          } else { 
            wx.showToast({
              title: res.data.code,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
      var _this = this;
      var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
      var timer = setInterval(function () {
        countDownNum -= 1;
        _this.setData({
          countDownNum: countDownNum
        })
        if (countDownNum <= -1) {
          clearInterval(timer);
          // 取消置顶的setInterval函数将要执行的代码
          _this.setData({
            countDownNum: 60,
          })
        }
      }, 1000)

    }
    setTimeout(() => {
      this.data.flag = true
    }, 5000)
  }
  },




  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
  
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})